package thread;

/**
 * Created with IntelliJ IDEA
 * Description:退出线程方法学习
 * User:22954
 * Date:2024-03-05
 * Time:23:16
 */
public class Exit {
    /*
    通过控制变量来终止线程
     */
    public static void main(String[] args) throws InterruptedException {
        T t = new T();
        t.start();
        Thread.sleep(10*1000);
        System.out.println("退出线程");
        t.setLoop(false);
    }
}
@SuppressWarnings("all")
class T extends Thread{
    private int count = 0;
    private boolean loop = true;

    @Override
    public void run() {
        while(loop) {
            System.out.println("线程正在运行" + (++count));
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}
